//
//  WBHomeViewController.swift
//  MyXinLangWeiBo
//
//  Created by 迟浩伦 on 2016/12/22.
//  Copyright © 2016年 迟浩伦. All rights reserved.
//

import UIKit
//定义全局常量
private let cellId = "cellId"
class WBHomeViewController: WBBaseViewController {

    lazy var statusList = [String]()
    //加载数据
    override func loadData() {
        
    //用网络工具加载微博数据
   WBNetworkManager.shared.statusList { (list, isSuccess) in
    print(list)
    //拿到数组 字典转模型 绑定数据
    
        }
        
        
        //模拟延时加载数据 -> dispatch_after
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
        
            for i in 1..<15 {
                
                if self.isPullup{
                //如果是上拉刷新 将数据追加到底部
                    self.statusList.append("上拉\(i)")
                }else{
                //每次将数据插入到数组顶部
                self.statusList.insert(i.description, at: 0)
                }
            }
            print("刷新")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标记
            self.isPullup = false
            
            self.tableView?.reloadData()
            
        }
       
        
    }

      func showFriends() {
        let vc = WBDemoViewController()
        
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
    

    }
//MARK: - 表格数据源方法
extension WBHomeViewController{

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        //设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        //返回cell
        return cell
    }
    
}
//MARK: - 设置界面
extension WBHomeViewController{
    
    override func setupTableView() {
        
        super.setupTableView()
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
       
        //注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }

}
    
